import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TeacherDialog extends JDialog {
    private JTextField idField;
    private JTextField nameField;
    private JTextField ageField;
    private JTextField subjectField;
    private JPasswordField passwordField;
    private JComboBox<String> scheduleBox;
    private Teacher teacher;

    public TeacherDialog(JFrame parent, String title, Teacher teacher) {
        super(parent, title, true);
        this.teacher = teacher;

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel idLabel = new JLabel("工号:");
        idLabel.setBounds(10, 10, 80, 25);
        panel.add(idLabel);

        idField = new JTextField(20);
        idField.setBounds(100, 10, 160, 25);
        panel.add(idField);

        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setBounds(10, 40, 80, 25);
        panel.add(nameLabel);

        nameField = new JTextField(20);
        nameField.setBounds(100, 40, 160, 25);
        panel.add(nameField);

        JLabel ageLabel = new JLabel("年龄:");
        ageLabel.setBounds(10, 70, 80, 25);
        panel.add(ageLabel);

        ageField = new JTextField(20);
        ageField.setBounds(100, 70, 160, 25);
        panel.add(ageField);

        JLabel subjectLabel = new JLabel("任职科目:");
        subjectLabel.setBounds(10, 100, 80, 25);
        panel.add(subjectLabel);

        subjectField = new JTextField(20);
        subjectField.setBounds(100, 100, 160, 25);
        panel.add(subjectField);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10, 130, 80, 25);
        panel.add(passwordLabel);

        passwordField = new JPasswordField(20);
        passwordField.setBounds(100, 130, 160, 25);
        panel.add(passwordField);

        JLabel scheduleLabel = new JLabel("课表:");
        scheduleLabel.setBounds(10, 160, 80, 25);
        panel.add(scheduleLabel);

        String[] schedules = { "上午 9 点-10 点", "上午 10 点-12 点", "下午 2 点到 4 点", "下午 4 点到 6 点" };
        scheduleBox = new JComboBox<>(schedules);
        scheduleBox.setBounds(100, 160, 160, 25);
        panel.add(scheduleBox);

        JButton saveButton = new JButton("保存");
        saveButton.setBounds(10, 190, 80, 25);
        panel.add(saveButton);

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveTeacher();
            }
        });

        if (teacher != null) {
            idField.setText(teacher.getId());
            nameField.setText(teacher.getName());
            ageField.setText(String.valueOf(teacher.getAge()));
            subjectField.setText(teacher.getSubject());
            passwordField.setText(teacher.getPassword());
            scheduleBox.setSelectedItem(teacher.getSchedule());
        }

        add(panel);
        setSize(300, 250);
        setLocationRelativeTo(parent);
    }

    private void saveTeacher() {
        if (teacher == null) {
            teacher = new Teacher();
        }

        teacher.setId(idField.getText());
        teacher.setName(nameField.getText());
        teacher.setAge(Integer.parseInt(ageField.getText()));
        teacher.setSubject(subjectField.getText());
        teacher.setPassword(new String(passwordField.getPassword()));
        teacher.setSchedule((String) scheduleBox.getSelectedItem());

        if (getTitle().equals("添加教师")) {
            ((TeacherManagementPanel) getParent()).addTeacher(teacher);
        } else {
            ((TeacherManagementPanel) getParent()).updateTeacher(teacher);
        }

        dispose();
    }
}
